package com.wj.jiangan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.Emp;
import com.wj.jiangan.pojo.Part;
import com.wj.jiangan.pojo.ext.PartExt;
import com.wj.jiangan.pojo.vo.PartVo;
import com.wj.jiangan.service.IClientService;
import com.wj.jiangan.service.IEmpService;
import com.wj.jiangan.service.IPartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 部门控制器
 *
 * @author wj
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/department")
public class PartController{

	@Autowired
	private IPartService partService;
	@Autowired
	private IEmpService empService;
	@Autowired
	private IClientService clientService;
	private Map<String,Object> map = new HashMap<String,Object>();

	/**
	 * 返回所有部门列表
	 *
	 * @return
	 */
	@GetMapping("/getAllPart")
	public Map<String,Object> getAllPart(Integer pIs){
		map.put("code", 0);
		map.put("data", partService.queryAllPart());
		return map;
	}


	/**
	 * @描述 分页查询员工表
	 * @创建人 dt
	 * @创建时间 2019/11/8
	 */
	@GetMapping("/findPartPage")
	public Map<String,Object> findpageClientNum(Page<Part> page){

		IPage<Part> partByPage = partService.findPartByPage(page);

		List<Map<String,Object>> list = new ArrayList<>();

		partByPage.getRecords().forEach(o->{
			HashMap<String,Object> map = new HashMap<>();
			map.put("pid", o.getPId());
			map.put("pname", o.getPName());
			list.add(map);
		});
		Map<String,Object> result = new HashMap<>();
		result.put("code", 0);
		result.put("message", "");
		result.put("count", partByPage.getTotal());
		result.put("data", list);
		return result;
	}


	/**
	 * 返回所有部门列表
	 *
	 * @return list
	 */
	@GetMapping("/partList")
	public List<Part> getAllPart(){
		return partService.queryAllPart();
	}

	/**
	 * 部门业绩饼图数据
	 *
	 * @return
	 */
	@GetMapping("/partPie")
	public List<PartExt> getPartPie(){
		return partService.departmentPerformancePie();
	}

	/**
	 * 添加一个部门
	 *
	 * @param part
	 */
	@RequestMapping("/parts")
	@ResponseStatus(HttpStatus.CREATED)
	public Map savePart(Optional<Part> part){
		System.err.println(part);
		partService.save(part.orElseThrow(IllegalStateException::new));
		map.put("code",0);
		return map;
	}

	/**
	 * 更新一个部门
	 * patch 更新某个指定资源的部分信息
	 * put 更新某个指定资源的全部信息
	 *
	 * @param pid
	 */
	@PatchMapping("/parts/{pid}")
	public void updatePart(@PathVariable int pid){

	}

	@GetMapping("/queryPartByPIs")
	public Map queryPartByPIs(Integer pIs){
		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("p_is",pIs);
		map.put("code",0);
		map.put("data",partService.queryPartByPIs(map2));
		return map;
	}

	@GetMapping("/queryInPage")
	public Map queryInPage(PartVo partVo){
		System.out.println(partVo.toString());
		QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
		partQueryWrapper.eq("p_is",partVo.getPIs());
		if(partVo.getPName() != null && partVo.getPName() != ""){
			partQueryWrapper.like("p_name",partVo.getPName());
		}
		if(partVo.getPRemark() != null && partVo.getPRemark() != ""){
			partQueryWrapper.like("p_remark",partVo.getPRemark());
		}
		Page<Part> partPage = new Page<>(partVo.getPage(),partVo.getLimit());
		IPage iPage = partService.queryInPage(partPage, partQueryWrapper);
		map.put("code",0);
		map.put("data",iPage.getRecords());
		map.put("count",iPage.getTotal());
		return map;
	}

	@RequestMapping("/searchPart")
	public Map searchPart(@ModelAttribute Part part){
		QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
		partQueryWrapper.eq("p_is",part.getPIs());
		if(part.getPName() != null){
			partQueryWrapper.like("p_name",part.getPName());
		}
		if(part.getPRemark() != null){
			partQueryWrapper.like("p_remark",part.getPRemark());
		}
		List<Part> parts = partService.searchPart(partQueryWrapper);
		map.put("code",0);
		map.put("data",parts);
		map.put("count",parts.size());
		return map;
	}
	@RequestMapping("/queryPartByName")
	public Map queryPartByName(String pName){
		QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
		partQueryWrapper.eq("p_name",pName);
		List<Part> parts = partService.queryPartByName(partQueryWrapper);
		map.put("code",0);
		map.put("tt",parts.size());
		return map;
	}
	@RequestMapping("/queryPartByPId")
	public Map queryPartByPId(Integer pId){
		System.out.println(pId);
		QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
		partQueryWrapper.eq("p_id",pId);
		List<Part> parts = partService.queryPartByPId(partQueryWrapper);
		System.out.println(parts);
		map.put("code",0);
		map.put("part",parts);
		return map;
	}
	@RequestMapping("/updatePartByPPId")
	public Map updatePartByPPId(Part part){
		partService.updatePartByPPId(part);
		map.put("code",0);
		return map;
	}

	@RequestMapping("/updatePIsByPId")
	public Map updatePIsByPId(Part part){
		map.put("code",0);
		//删除部门
		if(part.getPIs() == 1){
			List<Object> emps = empService.queryEmpByid(part.getPId());
			System.out.println("______________________________________________________________________________");
			System.out.println(emps);
			Integer clientIdByEmpIds = 0;
			if(emps.size() != 0){
				clientIdByEmpIds = clientService.findClientIdByEmpIds(emps);
			}
			System.out.println(clientIdByEmpIds);
			if(clientIdByEmpIds > 0 ){
				map.put("ct",1);
				return map;
			}else{
				part.setPIs(0);
				QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
				partQueryWrapper.eq("p_id",part.getPId());
				QueryWrapper<Emp> empQueryWrapper;
				if(emps.size() != 0){
					empQueryWrapper = new QueryWrapper<>();
					empQueryWrapper.in("e_id",emps);
					Emp emp = new Emp();
					emp.setEIs(0);
					empService.updateEmpIS(emp,empQueryWrapper);
				}
				partService.updatePartIS(part,partQueryWrapper);
				map.put("ct",0);
				return map;
			}
		}else{
			part.setPIs(1);
			QueryWrapper<Part> partQueryWrapper = new QueryWrapper<>();
			partQueryWrapper.eq("p_id",part.getPId());
			partService.updatePartIS(part,partQueryWrapper);
			map.put("ct",0);
			return map;
		}
	}

	@GetMapping("partlist")
	public List<Part> partlist(){
		List<Part> partQueryById = partService.findPartQueryById();
		return  partQueryById;
	}
}
